<?php
class Wee_Logging_Model_Event extends Mage_Core_Model_Abstract{
    const RESULT_SUCCESS = 'success';
    const RESULT_FAILURE = 'failure';
    public function __construct(){
        $this -> _init('wee_logging/event');
    }
    protected function _beforeSave(){
        if (!$this -> getId()){
            $this -> setStatus($this -> getIsSuccess() ? self :: RESULT_SUCCESS : self :: RESULT_FAILURE);
            if (!$this -> getUser() && $id = $this -> getUserId()){
                $this -> setUser(Mage :: getModel('admin/user') -> load($id) -> getUserName());
            }
            if (!$this -> hasTime()){
                $this -> setTime(time());
            }
        }
        $info = array();
        $info['general'] = $this -> getInfo();
        if ($this -> getAdditionalInfo()){
            $info['additional'] = $this -> getAdditionalInfo();
        }
        $this -> setInfo(serialize($info));
        return parent :: _beforeSave();
    }
    public function hasChanges(){
        if ($this -> getId()){
            return (bool)$this -> getResource() -> getEventChangeIds($this -> getId());
        }
        return false;
    }
}
